<?php
namespace app\index\model;
use think\Db;
use think\Loader;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 店铺业务处理
 */
class Shops extends Base{
	/**
	 * 分页
	 */
	public function pageQuery(){
		$areaIdPath = input('areaIdPath');
		$shopName = input('shopName');
		$where = [];
		$where['dataFlag'] = 1;
		if($areaIdPath !='')$where['areaIdPath'] = ['like',$areaIdPath."%"];
		if($shopName != '')$where['shopName|shopSn'] = ['like',"%$shopName%"];
		$page = $this->where($where)->order('shopId desc')->paginate(input('pagesize/d'))->toArray();
		if(count($page['Rows'])>0){
			foreach ($page['Rows'] as $key => $v){
				$page['Rows'][$key]['keeperName'] = model('Staffs')->where(['dataFlag'=>1,'staffRoleId'=>2,'shopId'=>$v['shopId']])->value('StaffName');
				$page['Rows'][$key]['clerkName'] = join(',',model('Staffs')->where(['dataFlag'=>1,'staffRoleId'=>3,'shopId'=>$v['shopId']])->column('StaffName'));
  				$page['Rows'][$key]['address'] = $this->areaPathName($v['areaIdPath']).' '.$v['shopAddress'];
			}
		}
		return $page;
	}
	
	public function areaPathName($areaIdPath){
		$areaIdPath = explode('_',$areaIdPath);
		$areaPathName = '';
		foreach($areaIdPath as $v){
			if($v)$areaPathName .= ' '.model('Areas')->getNames($v);
		}
		return $areaPathName;
	}
	/**
	 * 获取店铺信息
	 */
	public function getById($id){
		return $this->get($id);
	}
	/**
	 * 新增
	 */
	public function add(){
		$data = input('post.');
		$data["createTime"] = date('Y-m-d H:i:s');
        //获取地区
        $areaIds = model('Areas')->getParentIs($data['areaId']);
	    if(!empty($areaIds))$data['areaIdPath'] = implode('_',$areaIds)."_";
		$result = $this->validate('Shops.add')->allowField(true)->save($data);
        if(false !== $result){
        	if($data["staffId"]){
        		model('staffs')->where(['staffId'=>['in',$data["staffId"]]])->update(['shopId'=>$this->shopId]);
        	}
        	return HTRReturn("新增成功", 1);
        }else{
        	return HTRReturn($this->getError());
        }
	}
    /**
	 * 编辑
	 */
	public function edit(){
		$shopId = input('post.shopId/d',0);
		$data = input('post.');
        //获取地区
        $areaIds = model('Areas')->getParentIs($data['areaId']);
	    if(!empty($areaIds))$data['areaIdPath'] = implode('_',$areaIds)."_";
        HTRUnset($data,'shopId,createTime');
		$result = $this->validate('Shops.edit')->allowField(true)->save($data,['shopId'=>$shopId]);
        if(false !== $result){
        	model('staffs')->where(['shopId'=>$shopId])->update(['shopId'=>0]);
        	if($data["staffId"]){
        		model('staffs')->where(['staffId'=>['in',$data["staffId"]]])->update(['shopId'=>$shopId]);
        	}
        	return HTRReturn("编辑成功", 1);
        }else{
        	return HTRReturn($this->getError());
        }
	}	
	/**
	 * 删除
	 */
    public function del(){
	    $id = input('post.id/d');
		$data = [];
		$data['dataFlag'] = -1;
		Db::startTrans();
		try{
		    $result = $this->update($data,['shopId'=>$id]);
	        if(false !== $result){
		    	$result = model('stocks')->update($data,['shopId'=>$id]);
		    	if(false !== $result){
        			model('staffs')->where(['shopId'=>$id])->update(['shopId'=>0]);
		        	Db::commit();
		        	return HTRReturn("删除成功", 1);
		        }
	        }
		}catch (\Exception $e) {
            Db::rollback();
            return HTRReturn('删除失败');
        }
	}
}
